import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../provider/model/user_model.dart';
import 'api.dart';

class Account {
  static refreshAccount(BuildContext context) async {
    String? userId = context.read<UserModel>().userId;
    if (userId != '') {
      final prefs = await SharedPreferences.getInstance();
      String? accessToken = prefs.getString("accessToken");
      dynamic userInfoData = await Api.getInfo();
      dynamic userInfo = userInfoData['data'];
      dynamic userAccount = await Api.account();
      Provider.of<UserModel>(context, listen: false).setUserInfo(
          name: '${userInfo['nickname']}',
          id: '${userInfo['userId']}',
          mail: '${userInfo['email']}',
          imgUrl: '${userInfo['avatar']}',
          level: '${userInfo['vipLevel']}',
          token: accessToken,
          amount: '${userAccount['data']['accountAmount']}');
    }
  }
}
